import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'home',
        component: () => import('../views/Home.vue'),
        meta: { title: '首页' }
    },
    {
        path: '/category',
        name: 'category',
        component: () => import('../views/Category.vue'),
        meta: { title: '分类' }
    },
    {
        path: '/categoryArticle/:typeId',
        name: 'categoryArticle',
        component: () => import('../views/Home.vue'),
        meta: { title: '分类', params: 'typeId' }
    },
    {
        path: '/tag',
        name: 'tag',
        component: () => import('../views/Tag.vue'),
        meta: { title: '标签' }
    },
    {
        path: '/tagArticle/:tagId',
        name: 'tagArticle',
        component: () => import('../views/Home.vue'),
        meta: { title: '标签', params: 'tagId' }
    },
    {
        path: '/search/:words',
        name: 'search',
        component: () => import('../views/Home.vue'),
        meta: { title: '搜索', params: 'words' }
    },
    {
        path: '/about',
        name: 'about',
        component: () => import('../views/About.vue'),
        meta: { title: '关于' }
    },
    {
        path: '/friend',
        name: 'friend',
        component: () => import('../views/Friend.vue'),
        meta: { title: '友链' }
    },
    {
        path: '/article/:id',
        name: 'article',
        component: () => import('../views/Articles.vue'),
        meta: { title: '文章', params: 'id' }
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('../views/Login.vue'),
        meta: { title: '登录' }
    },
    { path: '/404', component: () => import('@/views/404') },
    { path: '/500', component: () => import('@/views/500') },
    //   { path: '/502', component: () => import('@/views/502') },
    { path: '/*', component: () => import('@/views/404') }
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})
router.beforeEach((to, from, next) => {
    let title = 'kebol'
    // if (to.meta.params) {
    //     title = `${to.meta.title}:${to.params[to.meta.params] || ''} - ${title}`
    // } else {
    //     title = `${to.meta.title} - ${title}`
    // }
    title = `${to.meta.title} - ${title}`
    document.title = title
    // if (to.path !== from.path) {
    //     store.dispatch('setLoading', true);
    // }
    next();
})
router.afterEach((to, from) => {
    window.scrollTo(0, 0);
    document.documentElement.scrollTop = document.body.scrollTop = 0;
    // window.scroll({ top: 0, left: 0, behavior: 'smooth' });
    // window.scrollTo(0, 0);
    // store.dispatch('setLoading', false);
})
export default router
